const serviceModel = require('../model/serviceModel');

//查询所有服务
module.exports.findService = async function (searchObj,page){
    if(page.size){
        //current当前页，size一页显示多少条数据
        let { current,size } = page;
        // 获取所有服务的总数
        let total = await serviceModel.countDocuments(searchObj);
        // 获取总页数 Math.ceil:向上取整
        let totalPage = Math.ceil(total / size);
        // 当前页显示的数据
        let rows = await serviceModel
            .find(searchObj)
            .populate({ path: 'adminId' })
            .limit(parseInt(size))//条数
            .skip( (current-1) * size);
        return { current, size, total, totalPage, rows };
    }else {
        return await serviceModel
            .find(searchObj)
            .populate({ path: 'adminId' })
    }
}
//增加服务
module.exports.insert = async function (service) {
    return await serviceModel.create(service);
}
//删除服务
module.exports.delete = async function (id){
    return await serviceModel.deleteOne( { _id: id } );
}
// 通过服务的id查询
module.exports.findById = async function ( id ){
    return await serviceModel.findById( { _id: id } );
}
// 修改服务内容
module.exports.update = async function (id,service){
    return await serviceModel.updateOne({ _id: id }, service );
}